package test.event;

import static junit.framework.Assert.*;
import game.net.server.Server;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class ServerShould {
	private Server server;

	@Before
	public void init(){
		server = new Server();	
	}
	
	@Test
	public void StartProperly() {
		
		// give the server a few seconds to spool up
		boolean timeout = false;
		long timestamp = System.currentTimeMillis();
		while (!timeout && !server.isActive()) {
			timeout = System.currentTimeMillis() - 3000 > timestamp;
			// Thread.yield();
		}
		
		assertTrue(server.isActive());
	}
	
	@Test
	public void StartEvenIfPortAlreadyInUse(){
		Server server2 = new Server();
		assertNotSame(server2.listeningPort(), server.listeningPort());
		
		//give the server a few seconds to spool up
		boolean timeout = false;
		long timestamp = System.currentTimeMillis();
		while(!timeout && !server2.isActive()){
			timeout = System.currentTimeMillis() - 3000 > timestamp;
			//Thread.yield();
		}
		
		assertTrue(server2.isActive());
	}
	
	@Test
	public void ShutdownWithoutExceptions(){
		server.shutdown();
		assertEquals(0, server.getErrorLog().size());
	}
	
	@Test
	public void CaptureErrors(){
		Server temp = new Server(-2);
		temp.shutdown();
		assertTrue(temp.getErrorLog().hasError("Port value out of range"));
	}
	
	@After
	public void uninit(){
		server.shutdown();
	}
}
